package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qf.clients.TeacherClients;
import com.qf.dao.CourseMapper;
import com.qf.dao.CourseRepository;
import com.qf.pojo.entity.QfCourse;
import com.qf.pojo.entity.QfCourseVo;
import com.qf.pojo.entity.QfTeach;
import com.qf.pojo.repVo.BaseResp;
import com.qf.pojo.repVo.QfCourseRepVo;
import com.qf.service.CourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    CourseRepository courseRepository;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    TeacherClients teacherClients;

    private static  final String CLASSFIY_COURSE="classfiy_course_";
    //根据分类查询课程
    @Override
    public BaseResp findCourseByClassify(Integer id, Integer page, Integer size) {
        //1.判断redis中是否有该分类的课程信息
        Boolean aBoolean = redisTemplate.hasKey(CLASSFIY_COURSE + id.toString());
        Integer start = (page-1)*size;
        List list=null;
        Long total=null;
        if (aBoolean){
            list = redisTemplate.opsForList().range(CLASSFIY_COURSE + id.toString(), start, start + size - 1);
            total = redisTemplate.opsForList().size(CLASSFIY_COURSE + id.toString());
        }else{
            //从数据库查询出所有的数据
            List<QfCourseVo> courseByClassfiy = courseMapper.findCourseByClassfiy(id);
            //将集合放置到redis中
            if (courseByClassfiy!=null&&courseByClassfiy.size()>0){
                redisTemplate.opsForList().rightPushAll(CLASSFIY_COURSE + id.toString(),courseByClassfiy);
                list = redisTemplate.opsForList().range(CLASSFIY_COURSE + id.toString(), start, start + size - 1);
                total = redisTemplate.opsForList().size(CLASSFIY_COURSE + id.toString());
            }else{
                redisTemplate.opsForList().rightPushAll(CLASSFIY_COURSE + id.toString(),"");
                redisTemplate.expire(CLASSFIY_COURSE + id.toString(),60, TimeUnit.SECONDS);
            }

        }


        return new BaseResp(1,"查询成功",list,total);
    }

    /**
     * 课程与讲师的关联关系
     * 1.Mybatsi的两表联查（联查的方式在企业中很少使用）
     * 2.先查course获取到教师id，查询出教师的信息
     * @param id
     * @return
     */
    @Override
    public BaseResp findCourseDetailById(Integer id) {
        //1.根据id查询出课程
        Optional<QfCourse> byId = courseRepository.findById(id);
        if (!byId.isPresent()){
            return new BaseResp(0,"没有该课程的信息!",null,null);
        }
        QfCourse qfCourse = byId.get();
        Integer teachid = qfCourse.getCourseTeachs();
        //通过feign远程调用teacher服务获取到该讲师的信息
        BaseResp teachBease = teacherClients.findById(teachid);
        Object data = teachBease.getData();
        QfCourseRepVo qfCourseRepVo = new QfCourseRepVo();
        //将查询出的数据copy到返回的对象中
        BeanUtils.copyProperties(qfCourse,qfCourseRepVo);
        if (data!=null){
            String s = JSONObject.toJSONString(data);
            QfTeach qfTeach = JSONObject.parseObject(s, QfTeach.class);
            qfCourseRepVo.setQfTeach(qfTeach);
        }
        return new BaseResp(1,"查询成功",qfCourseRepVo,null);
    }
}
